package texture;

import java.nio.IntBuffer;

public class TextureUtil {
	private static final IntBuffer dataBuffer = GLReferenceTexture.createDirectIntBuffer(4194304);
	public static final DynamicTexture missingTexture = new DynamicTexture(16, 16);
	public static final int[] missingTextureData = missingTexture.getTextureData();

	static void bindTexture(int par0) {
		GLReferenceTexture.glBindTexture(par0);
	}
	
	public static int glGenTextures() {
		return GLReferenceTexture.glGenTextures();
	}

	public static void allocateTexture(int par0, int par1, int par2) {
		GLReferenceTexture.glBindTexture(par0);
		GLReferenceTexture.glTexImage2D(par1, par2, (IntBuffer) null);
		// data may be a null pointer. In this case, texture memory is allocated to accommodate
		// a texture of width width and height height. You can then download subtextures to
		// initialize this texture memory. The image is undefined if the user tries to apply an
		// uninitialized portion of the texture image to a primitive.
	}

	public static void uploadTextureSub(int[] par0ArrayOfInteger, int width, int height, int xoffset, int yoffset, boolean par5, boolean par6) {
		int i1 = 4194304 / width;
		setTextureBlurred(par5);
		setTextureClamped(par6);
		int j1;

		for (int k1 = 0; k1 < width * height; k1 += width * j1) {
			int l1 = k1 / width;
			j1 = Math.min(i1, height - l1);
			int i2 = width * j1;
			copyToBufferPos(par0ArrayOfInteger, k1, i2);
			GLReferenceTexture.glTexSubImage2D(xoffset, yoffset + l1, width, j1, dataBuffer);
		}
	}

	private static void copyToBufferPos(int[] par0ArrayOfInteger, int par1, int par2) {
		int[] aint1 = par0ArrayOfInteger;

		if (GLReferenceTexture.anaglyph()) {
			aint1 = updateAnaglyph(par0ArrayOfInteger);
		}

		dataBuffer.clear();
		dataBuffer.put(aint1, par1, par2);
		dataBuffer.position(0).limit(par2);
	}

	public static int[] updateAnaglyph(int[] par0ArrayOfInteger) {
		int[] aint1 = new int[par0ArrayOfInteger.length];

		for (int i = 0; i < par0ArrayOfInteger.length; ++i) {
			int j = par0ArrayOfInteger[i] >> 24 & 255;
			int k = par0ArrayOfInteger[i] >> 16 & 255;
			int l = par0ArrayOfInteger[i] >> 8 & 255;
			int i1 = par0ArrayOfInteger[i] & 255;

			int j1 = (k * 30 + l * 59 + i1 * 11) / 100;
			int k1 = (k * 30 + l * 70) / 100;
			int l1 = (k * 30 + i1 * 70) / 100;

			aint1[i] = j << 24 | j1 << 16 | k1 << 8 | l1;
		}

		return aint1;
	}

	private static void setTextureClamped(boolean par0) {
		GLReferenceTexture.glTextureClamped(par0);
	}

	private static void setTextureBlurred(boolean par0) {
		GLReferenceTexture.glTextureBlurred(par0);
	}

}
